<?php

    function postNull ($string) {
        if (is_null($string) || $string == "") {
            return "null";
        } else {
            return '"'.$string.'"';
        }
    }

    function postKundenListe () {
        

        $query = mysql_query("SELECT *, Kunde.ID as KundeID, Adresse.ID as AdressID from Kunde join Adresse on Kunde.Adresse = Adresse.ID order by Kunde.Vorname asc");
        
        postKundenTabelle ($query);
    }

    function postKundenButtonLeiste () {
        echo '<div class="buttonleiste">';

        if (isset($_POST['neuerKunde']) || isset($_POST['aendernKunde']) || isset($_POST['resetid'])) {
            if (isset($_POST['aendernKunde'])) {
                $id = $_POST['aendernKunde'];
            } elseif (isset($_POST['resetid'])) {
                $id = $_POST['resetid'];
            }

            echo '<a href="index.php?section=kunden"><button class="submit left r10" name="kunden" type="submit">Zur Kunden&uuml;bersicht</button></a>';
            echo '<form action="index.php?section=kunden" method="post">
                    <input type="hidden" name="resetid" value="' . $id . '"> 
                    <button class="submit left r10">Reset</button>
                </form>';
            echo '<button class="submit left buttonIEdisable" name="speichernKunde" type="submit" form="kundenform">Speichern</button>';
       
        } elseif (isset($_POST['detailsKunden'])) {
            $id = $_POST['detailsKunden'];

            echo '<a href="index.php?section=kunden"><button class="submit left r10" name="alleKunden" type="submit">Alle Kunden anzeigen</button></a>';
            echo '<form action="index.php?section=kunden" method="post" id="detailsButton">
                    <input type="hidden" name="aendernKunde" value="' . $id . '"> 
                    <button class="submit left r10">&Auml;ndern</button>
                    <input type="hidden" name="angebotKunde" value="' . $id . '"> 
                    <button type="button" class="submit left r10" onclick="postKundeAngebot();">Angebote</button>
                    <input type="hidden" name="auftragKunde" value="' . $id . '"> 
                    <button type="button" class="submit left r10" onclick="postKundeAuftrag();">Auftr&auml;ge</button>
                    <input type="hidden" name="rechnungKunde" value="' . $id . '"> 
                    <button type="button" class="submit left r10" onclick="postKundeRechnung();">Rechnungen</button>
                    <input type="hidden" name="kundenanfrageKunde" value="' . $id . '"> 
                    <button type="button" class="submit left" onclick="postKundeKundenanfrage();">Kundenanfragen</button>
                </form>';

        } elseif (isset($_POST['versenden'])) {
            echo '<a href="index.php?section=kunden"><button class="submit left r10" name="kunden" type="submit">Zur Kunden&uuml;bersicht</button></a>';
            echo '<button class="submit left buttonIEdisable" name="sendenEmail" type="submit" form="emailformkunde">Senden</button>';

        } elseif (isset($_POST['sendenEmail'])) { 
            echo '<a href="index.php?section=kunden"><button class="submit left r10" name="kunden" type="submit">Zur Kunden&uuml;bersicht</button></a>';

        } else {
            if (isset($_POST['sucheKunde'])) {
                echo '<a href="index.php?section=kunden"><button class="submit left r10" name="alleKunden" type="submit">Alle Kunden anzeigen</button></a>';
            } 

            echo '<form method="post" action="index.php?section=kunden" class="left r10">
                <button class="submit" name="neuerKunde" type="submit">Kunde hinzuf&uuml;gen</button>
            </form>';

            echo '<a href="index.php?section=menu"><button class="submit left r10" name="menu" type="submit">Zum Men&uuml;</button></a>';

            echo '<form method="post" action="index.php?section=kunden" id="searchKunde" class="right">
                <input class="suche" name="suche" placeholder="Suche" pattern=".+" type="search" />
                <button class="submit" name="sucheKunde" type="submit">Suche</button>
            </form>';

            if(!isset($_POST['sucheKunde'])) {
                echo '<form method="post" action="index.php?section=kunden" id="typKundeBox" class="formular">
                    <select class="w160" id="typKunde" name="typKunde" size="1" onchange="chooseTyp(this);" required>
                        <option value="alle" selected="selected">Alle Kunden</option>
                        <option value="privat">Privatkunden</option>
                        <option value="firma">Firmenkunden</option>
                    </select>
                </form>'; 
            }


        }
        echo '</div>';
    }

    function postKundenTabelle ($query) {
        echo '<table class="liste clear">
            <tr>
                <th>#</th>
                <th>Vorname</th>
                <th>Nachname</th>
                <th>Firma</th>
                <th>Email</th>
                <th>Status</th>
                <th>&Auml;ndern</th>
            </tr>';
        for ($i=0; $i < mysql_num_rows ($query); $i++) { 
            $kunde = mysql_fetch_array($query);
            if ($i % 2 == 0) {
                echo '<tr>';
            } else {
                echo '<tr class="farbigeZeile">';
            }

            echo '<td>
                        <form action="index.php?section=kunden" method="post">
                            <input type="hidden" name="detailsKunden" value="' . $kunde['KundeID'] . '"> 
                            <button class="linkbutton">' . $kunde['KundeID'] . '</button>
                        </form>
                    </td>';
            echo '
                        <td>' . $kunde['Vorname'] . '</td>
                        <td>' . $kunde['Nachname'] . '</td>
                        <td>' . $kunde['Firma'] . '</td>
                        <td>
                        <form action="index.php?section=kunden" method="post">
                            <input type="hidden" name="versenden" value="' . $kunde['KundeID'] . '"> 
                            <button class="linkbutton">' . $kunde['EMail'] . '</button>
                        </form>
                        </td>';
            if ($kunde['Aktiv'] == '1') {
                echo '
                    <td>
                        <form action="index.php?section=kunden" method="post">
                            <input type="hidden" name="deaktivieren" value="' . $kunde['KundeID'] . '"> 
                            <button class="linkbutton">Deaktivieren</button>
                        </form>
                    </td>';
            } else {
                echo '
                    <td>
                        <form action="index.php?section=kunden" method="post">
                            <input type="hidden" name="aktivieren" value="' . $kunde['KundeID'] . '"> 
                            <button class="linkbutton">Aktivieren</button>
                        </form>
                    </td>';
            }        
            echo '
                    <td>
                        <form action="index.php?section=kunden" method="post">
                            <input type="hidden" name="aendernKunde" value="' . $kunde['KundeID'] . '"> 
                            <button class="linkbutton">&Auml;ndern</button>
                        </form>
                    </td>
                </tr>';
        }
        echo '</table>';
    }

    function postKundeBearbeiten ($kunde) {
        $query = mysql_query("SELECT *, Kunde.ID as KundeID, Adresse.ID as AdressID from Kunde join Adresse on Kunde.Adresse = Adresse.ID where Kunde.ID=" . $kunde);
        $row = mysql_fetch_array($query);
        postKunde($row);
    }

    function setLeerString ($string) {
        if($string == null) return "";
        else return $string;
    }

    function postKunde ($kunde) {
        

        echo '
            <form class="formular" action="index.php?section=kunden" method="post" id="kundenform" name="kundenform" onkeyup="kontaktdaten(); checkAbonnent(this);" onmousemove="kontaktdaten(); checkAbonnent(this);">
            <ul>
                <li>
                     <h2>Kunde  ';
            if (isset($kunde['KundeID'])) {
                echo '#' . $kunde['KundeID'] . ' &auml;ndern';
            } else {
                echo 'hinzuf&uuml;gen';
            }
                        
            echo '</h2>
                     <span class="required_notification">* Ben&ouml;tigte Felder</span>
                </li>
                <li>
                    <label for="anrede">Anrede:</label>
                    <input id="anrede" name="anrede" value="' . $kunde['Anrede'] . '" type="text" pattern="^[A-Z][a-z]{3,}$" placeholder="Anrede" maxlength="30" required />
                    <span class="form_hint">z.B. Herr, Frau</span>
                </li>
                <li>
                    <label for="vorname">Vorname:</label>
                    <input id="vorname" name="vorname" value="' . $kunde['Vorname'] . '" type="text" pattern="^[A-ZÄÜÖ][a-zäüö]+(([\s]|[-]){1}[A-ZÄÜÖ][a-zäüö]+)*$" placeholder="Vorname" maxlength="30" required />
                </li>
                <li>
                    <label for="nachname">Nachname:</label>
                    <input id="nachname" name="nachname" value="' . $kunde['Nachname'] . '" type="text" pattern="^[A-ZÄÜÖ][a-zäüö]+(([\s]|[-]){1}[A-ZÄÜÖ][a-zäüö]+)*$" placeholder="Nachname" maxlength="30" required />
                </li>
                <li>
                    <label for="firma">Firma:</label>
                    <input id="firma" name="firma" value="' . setLeerString($kunde['Firma']) . '" type="text" placeholder="Firma" maxlength="30" />
                    <input name="ustidnr" value="' . $kunde['USTIDNR'] . '" type="text" pattern="^ATU[0-9]{8}$" placeholder="UstIDNr" maxlength="30" /> 
                    <span class="form_hint">ATU12345678</span>
                </li>
                <li>
                    <label for="strasse">Strasse:</label>
                    <input id="strasse" name="strasse" value="' . $kunde['Strasse'] . '" type="text" pattern="^[A-ZÄÜÖ][a-zäüö]+(([\s]|[-])[A-Za-zÄÜÖäüö]+)*[\s][1-9]+[0-9]*([/][1-9]+[0-9]*)*$" placeholder="Strasse" maxlength="30" required />
                    <span class="form_hint">ß=ss</span>
                </li>
                <li>
                    <label for="plz">PLZ / Ort:</label>
                    <input type="hidden" name="adresseid" value="' . $kunde['AdressID'] . '">
                    <input id="plz" name="plz" value="' . $kunde['PLZ'] . '" type="text" placeholder="PLZ" maxlength="30" required />
                    <input id="ort" name="ort" value="' . $kunde['Ort'] . '" type="text" pattern="^[A-ZÄÜÖ][a-zäüö]+(([\s]|[-]){1}[A-ZÄÜÖ][a-zäüö]+)*$" placeholder="Ort" maxlength="30" required />
                </li>
                <li>
                    <label for="email">EMail:</label>
                    <input id="email" name="email" value="' . $kunde['EMail'] . '" type="email" placeholder="EMail" maxlength="100" required />
                    <span class="form_hint">test@test.at</span>
                </li>
                <li>
                    <label for="telefon">Telefonnr.:</label>';
            echo '<input id="telefon" name="telefon" value="' . $kunde['Telefon'] . '" type="tel" pattern="^[0-9]{4,20}$" placeholder="Telefonnr." maxlength="21" required />';
        echo '
                    <span class="form_hint">min 4 und max 20 Stellen</span>
                </li>
                <li>
                    <label for="aktiv">Aktiv:</label>
                    <select id="aktiv" name="aktiv" size="1" required>
                        <option></option>';
        if ($kunde['Aktiv'] == 0) {
            echo '<option value="' . 0 . '" selected="selected">Nein</option>';
        } else {
            echo '<option value="' . 0 . '">Nein</option>';
        }
        if ($kunde['Aktiv'] == 1 || !isset($kunde['Aktiv'])) {
            echo '<option value="' . 1 . '" selected="selected">Ja</option>';
        } else {
            echo '<option value="' . 1 . '">Ja</option>';
        }
        echo '
                    </select>
                    <input type="hidden" name="id" value="' . $kunde['KundeID'] . '"> 
                </li>
                <li>
                    <label for="abonnent">Abonnent:</label>';
        if ($kunde['Abonnent'] == 1){
            echo '<input id="abonnent" type="checkbox" name="abonnent" checked="checked" value="1" disabled>';
        } else {
            echo '<input id="abonnent" type="checkbox" name="abonnent" value="0" disabled>';
            // @TODO value - Fehler 
        }       
        
        echo '            
                </li>
                <li>
                    <button class="submit buttonIEenable" name="speichernKunde" type="submit">Speichern</button>
                </li>
            </ul>
        </form>
        ';
    }

    function postDetailsKunde ($kunde) {
        

        $query = mysql_query("SELECT *, Kunde.ID as KundeID, Adresse.ID as AdressID from Kunde join Adresse on Kunde.Adresse = Adresse.ID where Kunde.ID=" . $kunde);
        $kunde = mysql_fetch_array($query);

        echo '
        <table class="detailliste">
            <tr>';
        if ($kunde['Firma'] == "" or $kunde['Firma'] == null) {
            echo '<td colspan="2"><h2>' . $kunde['Vorname'] . ' ' . $kunde['Nachname'] . '</h2></td>';
        } else {
            echo '<td colspan="2"><h2>' . $kunde['Firma'] . '</h2></td>';
        }
        
        echo '        
            </tr>
            <tr>
                <td class="beschriftung">Adresse:</td>
                <td>' . $kunde['Strasse'] . ', ' . $kunde['PLZ'] . ' ' . $kunde['Ort'] . '</td>
            </tr>
            <tr>
                <td class="beschriftung">EMail:</td>
                
                <td>
                        <form action="index.php?section=kunden" method="post">
                            <input type="hidden" name="versenden" value="' . $kunde['KundeID'] . '"> 
                            <button class="linkbutton">' . $kunde['EMail'] . '</button>
                        </form>
                </td>
            </tr>
            <tr>
                <td class="beschriftung">Telefonnr.:</td>
                <td>' . $kunde['Telefon'] . '</td>
            </tr>
            <tr>
                <td class="beschriftung">Abonnent:</td>';
        if ($kunde['Abonnent'] == 1) {
            echo '<td>Ja</td>';
        } else {
            echo '<td>Nein</td>';
        }
        echo '
            </tr>
            <tr>
                <td class="beschriftung">Aktiv:</td>';
            if ($kunde['Aktiv'] == 1) {
                echo '<td>Ja</td>';
            } else {
                echo '<td>Nein</td>';
            }
            echo'</tr>
        </table>';
    }

    function postEmailForm ($kunde) {
        $query = mysql_query('SELECT * from kunde where id = ' . $kunde);
        $resultkunde = mysql_fetch_array($query);

        echo '
        <form class="formular" action="index.php?section=kunden" method="post" id="emailformkunde" name="emailformkunde">
        <ul>
            <li>
                <h2>Email an ' . $resultkunde['Anrede'] . ' ' . $resultkunde['Vorname'] . ' ' . $resultkunde['Nachname'] . '</h2>
                     <span class="required_notification">* Ben&ouml;tigte Felder</span>
                </li>
            <li>
                <label for="empf&auml;nger">Empf&auml;nger:</label>
                <input type="hidden" name="kundeEmail" value="' . $resultkunde['ID'] . '">
                <input id="empf&auml;nger" name="empf&auml;nger" value="' . $resultkunde['EMail'] . '" type="text" placeholder="Empf&auml;nger" maxlength="50" required />
            </li>
            <li>
                <label for="betreff">Betreff:</label>
                <input id="betreff" name="betreff" value="" type="text" placeholder="Betreff" maxlength="30" required />
            </li>
            <li>
                <label for="text">Text:</label>
                <textarea id="text" name="text" placeholder="Text" maxlength="500" required></textarea>
            </li>
            <li>
                <button class="submit buttonIEenable" name="sendenEmail" type="submit">Senden</button>
            </li>
        </ul>
        </form>';
    }

    function sendenKundeEmail ($kunde) {
        include ('lib/PHPMailer-master/class.phpmailer.php');

        $query = mysql_query('SELECT * from kunde where id = ' . $kunde);
        $kunde = mysql_fetch_array($query);

        $mail = new PHPMailer;

        $mail->IsSMTP();                                      // Set mailer to use SMTP
        $mail->Host = 'mail.gmx.net';  // Specify main and backup server
        $mail->SMTPAuth = true;                               // Enable SMTP authentication
        $mail->Username = '165209673';                            // SMTP username
        $mail->Password = 'martin-hifi';                           // SMTP password
        $mail->SMTPSecure = 'SSL';                            // Enable encryption, 'ssl' also accepted
        
        $mail->From = 'martin-hifi@gmx.at';
        $mail->FromName = 'Felix-Martin-Hifi';
        $mail->WordWrap = 50;                                 // Set word wrap to 50 characters
        $mail->IsHTML(true);                                  // Set email format to HTML
        
        $mail->Subject = $_POST['betreff'];

        if($kunde['Anrede'] == "Herr") {
            $text = 'Sehr geehrter Herr ' . $kunde['Vorname'] . ' ' . $kunde['Nachname'] . '!</br></br>' . $_POST['text'];
        } elseif ($kunde['Anrede'] == "Frau") {
            $text = 'Sehr geehrte Frau ' . $kunde['Vorname'] . ' ' . $kunde['Nachname'] . '!</br></br>' . $_POST['text'];
        } else {
            $text = 'Sehr geehrte Damen und Herren!</br></br>' . $_POST['text'];
        }

        $mail->Body = $text;
        $mail->AddAddress($kunde['EMail']);  // Add a recipient
        if(!$mail->Send()) {
            echo '
            <tr>';
            if ($kunde['Anrede'] == 'Firma') {
                echo '<div class="error">Die Email wurde nicht an die ' . $kunde['Anrede'] . ' ' . $kunde['Firma'] . ' versandt. Mail-Error: ' . $mail->ErrorInfo . '</div>';
            } else {
                echo '<div class="error">Die Email wurde nicht an ' . $kunde['Vorname'] . ' ' . $kunde['Nachname'] . ' versandt. Mail-Error: ' . $mail->ErrorInfo . '</div>';
            }
        } else {
            if ($kunde['Anrede'] == 'Firma') {
                echo '<div class="info">Die Email wurde erfolgreich an die ' . $kunde['Anrede'] . ' ' . $kunde['Firma'] . ' versandt.</div>';
            } else {
                echo '<div class="info">Die Email wurde erfolgreich an ' . $kunde['Vorname'] . ' ' . $kunde['Nachname'] . ' versandt.</div>';
            }
        }
    }

?>